package com.woniuxy.credit_loans.staff_ma.service.impl;

import com.woniuxy.credit_loans.entity.User;
import com.woniuxy.credit_loans.mapper.UserMapper;
import com.woniuxy.credit_loans.staff_ma.dto.UserDto;
import com.woniuxy.credit_loans.staff_ma.service.UserService;
import com.woniuxy.credit_loans.staff_ma.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectByName(String userName) {
        User user = userMapper.selectByName(userName);
        return  user;
    }

    @Override
    public List<User> selectAll() {
        List<User> users = userMapper.selectAll();
        return  users;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User login(UserDto dto) {
        User user = userMapper.selectByUserAcc(dto.getUserAcc());
        if(user.getUserOpen() == 1){
            return userMapper.login(dto);
        } else {

            return user;
        }
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public User selectByUserAcc(String userAcc) {
        return userMapper.selectByUserAcc(userAcc);
    }

    @Override
    public int register(User user) {
        User user1 = userMapper.selectByUserAcc(user.getUserAcc());
        if (user1 == null){
            if(user.getUserPassword().equals(user.getUserBy2())){
                user.setUserBy2("");
                user.setUserName("游客");
                user.setUserOpen(1);
                user.setUserSex("男");
                return userMapper.register(user);
            } else {
                return 2;
            }
        }else {
            return 0;
        }
    }

    @Override
    public UserVo selectBorrowHistory(UserVo userVo) {
        return userMapper.selectBorrowHistory(userVo);
    }

    @Override
    public User selectByUser(User user) {
        return userMapper.selectByUser(user);
    }

    @Override
    public UserDto updateUserPwd(Long userId) {
        return userMapper.updateUserPwd(userId);
    }


}
